<?php

namespace App\Services;
use App\Models\AgentAccount;
use App\Models\AgentAccountDetail;
use App\Models\Withdraw;
use Illuminate\Support\Facades\DB;

class WithdrawService
{

    public function action($withdraw,$state=1,$remark=''){
        $res['state'] = false;
        $res['msg'] = '';
        if($state==1){
            $res = $this->agree($withdraw,$state=1,$remark);
        }else if($state==-1){
            $res = $this->reject($withdraw,$state=-1,$remark);
        }
        else{
            $res = $this->back($withdraw,$state=0,$remark);
        }
        return $res;
    }

    /**
     * 通过审核
     *
     * @param [type] $agent_id
     * @return void
     */
    public function agree($withdraw,$state=1,$remark=''){
        $res['state'] = false;
        $res['msg'] = '';
        if($withdraw->state!=0){
            $res['msg'] = "已审核，不能重复操作";
            return $res;
        }
        DB::beginTransaction();
        try{
            $withdraw->update(['state' => $state,'remark' => $remark]);
            $account = AgentAccount::where('agent_id',$withdraw->agent_id)->first();
            // if($account->balance<$withdraw->number){
            //     DB::rollBack();
            //     $res['msg'] = "代理{$withdraw->agent_id}余额不足";
            //     return $res;
            // }

            //创建日志详情
            AgentAccountDetail::create([
                'agent_id'=>$withdraw->agent_id,
                'number'=>$withdraw->number,
                'type'=>-1,
                'video_id'=>0,
            ]);
            DB::commit();
            $res['state'] = true;
            return $res;
        }catch(Exception $e){
            DB::rollBack();
            $res['msg'] = "操作失败，请重试";
            return $res;
        }
    }


    /**
     * 拒绝审核
     *
     * @param [type] $agent_id
     * @return void
     */
    public function reject($withdraw,$state=-1,$remark='平台拒绝'){
        $res['state'] = false;
        $res['msg'] = '';
        if($withdraw->state!=0){
            $res['msg'] = "已审核，不能重复操作";
            return $res;
        }
        DB::beginTransaction();
        try{
            $withdraw->update(['state' => $state,'remark' => $remark]);
            $account = AgentAccount::where('agent_id',$withdraw->agent_id)->first();
            $account->increment('balance', $withdraw->number);
            $account->decrement('withdrawed', $withdraw->number);
            DB::commit();
            $res['state'] = true;
            return $res;
        }catch(Exception $e){
            DB::rollBack();
            $res['msg'] = "操作失败，请重试";
            return $res;
        }
    }



    /**
     * 回退审核
     *
     * @param [type] $agent_id
     * @return void
     */
    public function back($withdraw,$state=0,$remark=''){
        if($withdraw->state==1){
            $res['msg'] = "已审核打款，不能重复操作";
            return $res;
        }
        $res['state'] = false;
        $res['msg'] = '';
        DB::beginTransaction();
        try{
            $withdraw->update(['state' => $state,'remark' => $remark]);
            DB::commit();
            $res['state'] = true;
            return $res;
        }catch(Exception $e){
            DB::rollBack();
            $res['msg'] = "操作失败，请重试";
            return $res;
        }
    }
}